package cn;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.seimicrawler.xpath.JXDocument;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

public class Main {

    public static void main(String[] args) throws IOException {

        CloseableHttpClient httpClient = HttpClients.createDefault();


        HttpGet httpGet = new HttpGet("http://www.whbsz.com.cn/Price.aspx?PageNo=1");

        CloseableHttpResponse response = httpClient.execute(httpGet);

        String html =EntityUtils.toString(response.getEntity());

        System.out.println(html);

        JXDocument document = JXDocument.create(html);
        File file = new File("src/main/resources/武汉白沙洲农产品.html");
        PrintStream ps = new PrintStream(new FileOutputStream(file));
        ps.println(html);


//        Elements elements = document.selNOne("/body").asElement().select("tr.center");
//        String[] names ={"产品","产地/规格","最高价","最低价","平均价"};
//        for (Element element : elements) {
//            String[] strings = element.text().split("\\s+");
//            StringBuilder productInfo = new StringBuilder();
//            for (int i = 0; i < 5; i++) {
//                productInfo.append(names[i]).append(":").append(strings[i]).append("\t");
//            }
//            System.out.println(productInfo);
//        }
//        Object nextPage =document.selOne("//span[@id='cphRight_lblPage']/a[1]/@href");
//        System.out.println(nextPage);
    }
}